package es.uned.hoomgle.web.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ResultPath;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import es.uned.hoomgle.core.services.interfaces.ParameterServiceI;
import es.uned.hoomgle.hibernate.beans.interfaces.ParameterI;


@ResultPath(value="/")
public class ParameterAction extends ActionSupport {

	
	@Resource
	ParameterServiceI parameterService;
	
	public List<ParameterI> parameters;
	
	public String [] isActiveArray;

	
	/**
	 * Obtained all permits administered
	 * @return String "success"
	 */
	public String setParameters (){
		parameters = parameterService.getAllParameters();
		return "success";
	}
	
	/**
	 * Update the parameters with the selected values in jsp
	 * @return String "success"
	 */
	public String saveParameters(){
		setParameters();
		Map  req = ActionContext.getContext().getParameters();
		for (ParameterI parameter : parameters) {
			boolean found = false;
			if(isActiveArray!=null){							
				for (int i = 0; i < isActiveArray.length && !found; i++) {							
					if(isActiveArray[i].equals(parameter.getId().toString())){
						found = Boolean.TRUE;
						parameter.setIsActive(Boolean.TRUE);
					}
				}
			}
			if(!found){
				parameter.setIsActive(Boolean.FALSE);
			}			
		}		
		parameterService.saveList(parameters);
		addActionMessage("Permisos guardados!");
		return "success";
	}
	
}
